﻿using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections;

namespace YN.Utils
{
    public class JsonHelper
    {
        private const string DateTimeFormat = "yyyy-MM-dd HH:mm";

        private class Root<T>
        {
            public int totalCount;
            public T root;
        }

        public static string ConvertListToJson<T>(T root, int totalCount)
        {
            if (root == null)
            {
                return string.Empty;
            }

            Root<T> json = new Root<T>();
            json.root = root;
            json.totalCount = totalCount;

            IsoDateTimeConverter format = new IsoDateTimeConverter();
            format.DateTimeFormat = DateTimeFormat;

            return JsonConvert.SerializeObject(json, format);
        }

        public static string ConvertListToJson<T>(T root)
        {
            if (root == null)
            {
                return string.Empty;
            }

            return ConvertListToJson(root, 0);
        }

    }

}
